package com.bjesc_b2c.app.appraiset.widget.popupwindow;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.CarfromOneAdapter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.GetCarForPresenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.GetCarForPresenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.CarForView;
import com.bjesc_b2c.app.appraiset.widget.BasePopupWindow;
import com.bjesc_b2c.app.appraiset.widget.view.IndexView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import master_bean.CarForBean;

/**
 * Created by yidong on 2017/6/13.
 */

public class MarketDealerPop  extends BasePopupWindow implements MultiItemTypeAdapter.OnItemClickListener, IndexView.OnSetIndexListenre,CarForView{
    private RecyclerView recyclerView;
    private IndexView indexView;
    private CarfromOneAdapter oneAdapter;
    private List<CarForBean> list = new ArrayList<CarForBean>();
    private LinearLayoutManager manager;
    private Context context;
    private DealearSelectListener selectListener;
    private int type;
    private GetCarForPresenter carForPresenter;
    public MarketDealerPop(Context context,List<CarForBean> brandList,DealearSelectListener selectListener,int type) {
        super(context,null);
        this.context = context;
        list.clear();
        list.addAll(brandList);
        this.selectListener = selectListener;
        this.type = type;
        oneAdapter = new CarfromOneAdapter(context, R.layout.layout_item_carfrom_one, list, type);
        indexView.setVisibility(View.GONE);
        manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(oneAdapter);
        oneAdapter.setOnItemClickListener(this);
    }
    @Override
    protected void initViews() {
        view = inflater.inflate(R.layout.layout_pop_car_brand,null);
        recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view);
        indexView = (IndexView) view.findViewById(R.id.index_view);
        indexView.setOnSetIndexListenre(this);
    }
    private void shortList(){
        Collections.sort(list, new Comparator<CarForBean>() {
            public int compare(CarForBean carBrandBean1, CarForBean carBrandBean2) {
                if (carBrandBean1.getFirstLetter().charAt(0) > carBrandBean2.getFirstLetter().charAt(0)) {
                    return 1;
                }
                if (carBrandBean1.getFirstLetter().charAt(0) == carBrandBean2.getFirstLetter().charAt(0)) {
                    return 0;
                }
                return -1;
            }
        });
        CarForBean carBean = new CarForBean();
        carBean.setId("0");
        carBean.setName("全部");
        list.add(0,carBean);
    }
    @Override
    protected void setView() {
        super.setView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
    }

    @Override
    protected int setRootViewId() {
        return R.id.root;
    }

    @Override
    public void getCarForDealer(List<CarForBean> listbean) {
        if(listbean.size()>0){
            type = 1;
            list.clear();
            list.addAll(listbean);
            oneAdapter = new CarfromOneAdapter(context, R.layout.layout_item_select_car_brand, list, type);
            recyclerView.setAdapter(oneAdapter);
            oneAdapter.setOnItemClickListener(this);
//          recyclerView.setOnScrollListener(scrollListener);
//          indexView.setVisibility(View.VISIBLE);
        }
    }

    private RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int position = manager.findFirstVisibleItemPosition();
            indexView.refresh(list.get(position+1).getFirstLetter().charAt(0));
        }
    };


    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        if(type == 3){
            carForPresenter = new GetCarForPresenterImpl(context, this);
            carForPresenter.getCarFor(list.get(position).getId());
        }else {
            for (int i = 0; i < list.size(); i++) {
                if (i == position) {
                    list.get(i).setCheck(true);
                } else {
                    list.get(i).setCheck(false);
                }
            }
            selectListener.getDealear(list.get(position).getId(), list.get(position).getName());
            oneAdapter.notifyDataSetChanged();
            dismiss();
        }
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
    public interface DealearSelectListener{
        void getDealear(String id,String name);
    }

    @Override
    public void setIndexListenre(char index) {
        for (int i = 0; i < list.size(); i++) {
            if (String.valueOf(index).equals(list.get(i).getFirstLetter())) {
                manager.scrollToPositionWithOffset(i, 0);
                break;//加了break显示才会更准确—yd
            }
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}


